﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using TPLOAWebClient.Resources;
using System.Web.Security;

namespace TPLOAWebClient
{
    public partial class studentprofile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = (HttpClient)Session["httpclient"];
            HttpResponseMessage response = null;

            if (Session["username"] != null)
            {
                response = client.GetAsync("api/members/" + Session["username"]).Result;

                var member = response.Content.ReadAsAsync<Member>().Result;
                if (member.UserType.Equals("Clinic"))
                {
                    Response.Redirect("clinicprofile.aspx");
                }
                else if (member.UserType.Equals("Admin"))
                {
                    Response.Redirect("adminprofile.aspx");
                }
                else if (member.UserType.Equals("Staff"))
                {
                    Response.Redirect("staffprofile.aspx");
                }
            }

            if (!Page.IsPostBack)
            {
                response = client.GetAsync("api/Students/" + Session["username"]).Result;  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body. Blocking!
                    var student = response.Content.ReadAsAsync<Student>().Result;
                    adminnoLbl.Text = student.AdminNo;
                    nameLbl.Text = student.Name;
                    emailTbx.Text = student.Email;
                    coursecodeLbl.Text = student.CourseCode;
                    caregroupLbl.Text = student.CareGroup;
                    homenoTbx.Text = student.HomeNo;
                    handphonenoTbx.Text = student.HandphoneNo;
                    classificationLbl.Text = student.Classification;
                }
                else
                {
                    updateLbl.Text += response.StatusCode + "(" + response.ReasonPhrase + ")";
                }
            }

        }

        protected void studentprofileBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("studentprofile.aspx");
        }

        protected void viewloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("studentviewloa.aspx");
        }

        protected void submitloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("studentsubmitloa.aspx");
        }

        protected void faqBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("studentfaq.aspx");
        }

        protected void otherservicesBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("studentotherservice.aspx");
        }

        protected void logoutBtn_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("default.aspx");
        }

        protected void updateBtn_Click(object sender, EventArgs e)
        {
            HttpClient client = (HttpClient)Session["httpclient"];

            var student = new Student()
            {
                AdminNo = adminnoLbl.Text,
                Name = nameLbl.Text,
                Email = emailTbx.Text,
                CourseCode = coursecodeLbl.Text,
                CareGroup = caregroupLbl.Text,
                HomeNo = homenoTbx.Text,
                HandphoneNo = handphonenoTbx.Text,
                Classification = classificationLbl.Text
            };

            HttpResponseMessage response = client.PutAsJsonAsync("api/Students/" + adminnoLbl.Text, student).Result;  // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                updateLbl.Visible = true;
                updateLbl.Text = "Student information is successfully updated.";
            }
            else
            {
                updateLbl.Visible = true;
                updateLbl.Text = "Error! Information cannot be updated.";
            }
        }
    }
}